<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.example.utils.MybatisUtils" %>
<%@ page import="com.example.dao.StudentMapper" %>
<%@ page import="com.example.pojo.Student" %>
<%@ page import="com.example.pojo.StuClass" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>更新学生信息</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $.ajax({
                url: 'GetClassesServlet',
                type: 'GET',
                dataType: 'json',
                success: function(data) {
                    var select = $('#class_name');
                    $.each(data, function(index, item) {
                        select.append('<option value="' + item + '">' + item + '</option>');
                    });
                    console.log(data);
                }
            });
        });
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        form {
            width: 300px;
            margin: 0 auto;
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        label {
            font-weight: bold;
        }
        input[type="text"], select {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<%
    String studentId = request.getParameter("id");
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = mapper.getStudentById(Integer.parseInt(studentId));
    SqlSession sqlSession1 = MybatisUtils.getSqlSession();
    StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
    StuClass stuClass = mapper1.getClassByClassId(student.getClass_id());
%>
<form id="updateForm" action="UpdateStudentServlet" method="post">
    <label for="stu_name">姓名：</label><br>
    <input type="text" id="stu_name" name="stu_name" value="<%= student.getStu_name() %>"><br> <!-- 显示学生姓名 -->
    <label for="stu_no">学号：</label><br>
    <input type="text" id="stu_no" name="stu_no" value="<%= student.getStu_no() %>"><br> <!-- 显示学生学号 -->
    <label for="class_name">班级：</label><br>
    <input type="hidden" name="stu_id" value="<%= student.getStu_id() %>">
    <select id="class_name" name="class_name">
        <option value="<%= stuClass.getClass_name() %>"><%= stuClass.getClass_name() %></option>
    </select><br><br>
    <input type="submit" value="更新学生">
</form>

<script>
    document.getElementById("updateForm").onsubmit = function(event) {
        // 获取表单字段的值
        var stuName = document.getElementById("stu_name").value.trim();
        var stuNo = document.getElementById("stu_no").value.trim();

        // 验证学号是否为整数且长度不超过整数范围
        if (stuNo !== "") {
            if (isNaN(stuNo) || parseInt(stuNo) > 2147483647) { // 2147483647 是 INT_MAX
                alert("学号必须是长度不能超过整数范围的整数");
                return false;
            }
        }

        // 验证姓名是否为字符串
        if (stuName !== "") {
            if (!isNaN(stuName)) {
                alert("姓名必须是字符串");
                return false;
            }
        }

        // 其他验证（如必填字段）可以在这里添加

        // 如果通过验证，则允许提交表单
        return true;
    };
</script>

</body>
</html>
